package com.base;


import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

import com.utils.Encrypter;
import com.utils.GenericResourceLoader;
import com.utils.Logger;

public class Config {

    public static String fileName = "conf/database.cfg";
    static Logger logger = new Logger(Config.class.getName());;
    
    /**
     * Contains the parameters and default values for this gateway
     * such as system id, password, default npi, and ton of sender...
     */
    private static Properties properties = new Properties();
    
    /**
     * Loads configuration parameters from the file with the given name.
     * Sets private variable to the loaded values.
     */
    public static void loadProperties(String fileName) throws IOException {
    	logger.log("Reading configuration file " + fileName + "...");
        InputStream in = GenericResourceLoader.loadResource(fileName);
        properties.clear();
        properties.load(in);
        in.close();
    }

    /** Gets a property and converts it into integer. */
    public static int getIntProperty(String propName, int defaultValue) {
        return Integer.parseInt(properties.getProperty(propName, Integer.toString(defaultValue)));
    }
    
    /** Gets a property and converts it into long. */
    public static long getLongProperty(String propName, long defaultValue) {
        return Long.parseLong(properties.getProperty(propName, Long.toString(defaultValue)));
    }
    
    public static String getStringProperty(String key, String defaultValue, boolean isEncrypt) {
    	String value = null;
    	try {
        	value = properties.getProperty(key, defaultValue);
        	if (isEncrypt) {
        		value = Encrypter.decrypt(value);
        	}
		} catch (Exception e) {
			value = defaultValue;
			logger.error("Loi LoadConfig::getStringProperty: " + e.toString());
		}
        return value;
    }

    static {
 		try{
 			loadProperties(fileName);	
 		}catch(IOException e){
 			e.printStackTrace();
 		}
    }

    public static void main(String[] args) {
		System.out.println(Config.getStringProperty("poolName", "Unknown", false));
	}
}
